﻿using MongoDB;
using System;
using System.Linq;
using Resn.WhuPub.DML;
using System.Collections.Generic;

namespace Resn.WhuPub.DAL
{
        public class DBStorage
        {
                #region 构造函数

                private DBStorage()
                {
                        //Mongo = new Mongo("mongodb://219.140.164.222:25566");
                        Mongo = new Mongo("mongodb://127.0.0.1:25566");

                        MongoDatabase = Mongo.GetDatabase("WhuPubCRM");

                        Mongo.Connect();
                }

                #endregion 构造函数

                #region 属性

                private static readonly DBStorage instance = new DBStorage();

                public static DBStorage Instance
                {
                        get { return instance; }
                }

                public Mongo Mongo { get; private set; }

                public IMongoDatabase MongoDatabase { get; private set; }



                #endregion 属性

                public void Insert<T>(T t) where T:class,IIdentity
                {

                        IMongoCollection<T> collection = MongoDatabase.GetCollection<T>();

                        if (DBCheck.ExistId<T>(t.Id))
                        {
                                throw new InvalidOperationException("该标识已存在，无法添加");
                        }

                        collection.Insert(t);

                        
                        
                }

                public void Delete<T>(T t) where T : class,IIdentity
                {

                        IMongoCollection<T> collection = MongoDatabase.GetCollection<T>();

                        if (!DBCheck.ExistId<T>(t.Id))
                        {
                                throw new InvalidOperationException("该标识不存在，无法删除");
                        }

                        collection.Remove(t);

                        
                }

                public void Delete<T>(string id) where T : class,IIdentity
                {

                        IMongoCollection<T> collection = MongoDatabase.GetCollection<T>();

                        if (!DBCheck.ExistId<T>(id))
                        {
                                throw new InvalidOperationException("该标识不存在，无法删除");
                        }

                        collection.Remove(i => i.Id == id);

                        
                }



                public void Update<T>(T t) where T : class,IIdentity
                {

                        IMongoCollection<T> collection = MongoDatabase.GetCollection<T>();

                        if (!DBCheck.ExistId<T>(t.Id))
                        {
                                throw new InvalidOperationException("该标识不存在，无法更新");
                        }

                        collection.Update(t, (i=>i.Id == t.Id));

                        
                }

                public T Select<T>(string id) where T : class,IIdentity
                {

                        IMongoCollection<T> collection = MongoDatabase.GetCollection<T>();

                        var result = collection.FindOne(i => i.Id == id);

                        

                        return result;
                }

                public List<T> Select<T>() where T : class,IIdentity
                {

                        IMongoCollection<T> collection = MongoDatabase.GetCollection<T>();

                        var result =  collection.Linq().ToList();

                        

                        return result;
                }
        }
}